﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Dapper;
using OddUserManager.Model;
using OddUserManager.Data.IRepositories;

namespace OddUserManager.Data.Repositories
{
    //public class RoleRepository : BaseRepository<Role, int>, IRoleRepository
    //{
    //    public RoleRepository(OddUserManagerDbContext dbContext) : base(dbContext)
    //    {
    //    }

    //    public List<int> GetAllMenuListByRole(int roleId)
    //    {
    //        var roleMenus = _dbContext.Set<RoleMenus>().Where(it => it.RoleId == roleId);
    //        var menuIds = from t in roleMenus select t.MenuId;
    //        return menuIds.ToList();
    //    }

    //    public bool UpdateRoleMenu(int roleId, List<RoleMenus> roleMenus)
    //    {
    //        var oldDatas = _dbContext.Set<RoleMenus>().Where(it => it.RoleId == roleId).ToList();
    //        oldDatas.ForEach(it => _dbContext.Set<RoleMenus>().Remove(it));
    //        _dbContext.SaveChanges();
    //        _dbContext.Set<RoleMenus>().AddRange(roleMenus);
    //        _dbContext.SaveChanges();
    //        return true;
    //    }
    //}


    public class RoleRepository : DapperBaseRepository<Role,int>, IRoleRepository
    {
        public RoleRepository()
        {
        }


        //public Role Get(int id)
        //{
        //    Role t = null;
        //    using (var con = GetOpenConnection())
        //    {
        //        var sql = $"select * from {typeof(Role).Name} where Id={id.ToString()};" +
        //            $"select * from RoleMenus where RoleId={id}";
        //        t = con.QueryMultiple(sql, null, null).Map<Role, RoleMenus, int>
        //              (
        //              m => m.Id,
        //              c => c.RoleId,
        //              (m, cs) => { m.RoleMenus = cs.ToList(); }
        //              ).FirstOrDefault();
        //    }
        //    return t;
        //}




        public bool InsertOrUpdate(Role entity)
        {
            var t = Get(entity.Id);
            if (null == t)
            {
                return Insert(entity);
            }
            else
            {
                return Update(entity);
            }
        }

        //public IEnumerable<Role> GetAllList()
        //{
        //    List<Role> list = null;
        //    using (var con = GetOpenConnection())
        //    {
        //        var sql = $"select * from Role;select * from RoleMenus;";
        //        list = con.QueryMultiple(sql, null, null).Map<Role, RoleMenus, int>
        //              (
        //              m => m.Id,
        //              c => c.RoleId,
        //              (m, cs) => { m.RoleMenus = cs.ToList(); }
        //              ).ToList();
        //    }
        //    return list;
        //}

        public IEnumerable<Role> LoadPageList(int startPage, int pageSize, out int rowCount, Func<Role, bool> where, Func<Role, object> order)
        {
            var result = GetAllList();
            if (where != null)
                result = result.Where(where);
            if (order != null)
                result = result.OrderBy(order);
            else
                result = result.OrderBy(m => m.Id);
            rowCount = result.Count();
            return result.Skip((startPage - 1) * pageSize).Take(pageSize);
        }
        


        public List<int> GetAllMenuListByRole(int roleId)
        {
            List<int> list = null;
            using (var con = GetOpenConnection())
            {
                var sql = $"select MenuId from RoleMenus where RoleId={roleId}";
                list = con.Query<int>(sql).ToList();
            }
                return list;
        }

        public bool UpdateRoleMenu(int roleId, List<RoleMenus> roleMenus)
        {
            bool isSuccess = false;
            using (var con = GetOpenConnection())
            {
                var trans = con.BeginTransaction();
                try {
                    var deletesql = $"delete from RoleMenus where RoleId={roleId}";
                    con.Execute(deletesql, null, trans, 3000, null);
                    StringBuilder sb = new StringBuilder();
                    roleMenus.ForEach(p =>
                    {
                        sb.Append($"insert into RoleMenus (RoleId,MenuId) values({p.RoleId},{p.MenuId});");
                    });
                    con.Execute(sb.ToString(), null, trans, 3000, null);
                    trans.Commit();
                    isSuccess = true;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw ex;
                }
            }
            return isSuccess;
        }
    }
}
